package com.slofzx.nuoda.evaluation.param.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.slofzx.nuoda.common.util.IdGen;
import com.slofzx.nuoda.evaluation.param.entity.CommonFixed;
import com.slofzx.nuoda.evaluation.param.dto.CommonFixedDTO;
import com.slofzx.nuoda.evaluation.param.vo.CommonFixedVO;
import com.slofzx.nuoda.evaluation.param.dao.CommonFixedDAO;
import com.slofzx.nuoda.evaluation.param.service.CommonFixedService;
import com.slofzx.nuoda.evaluation.project.util.BusiUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.slofzx.nuoda.common.enums.CommonResultEnum;
import com.slofzx.nuoda.common.enums.ServiceResult;
import com.slofzx.nuoda.common.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 公共固定参数服务实现类
 *
 * @author nuoda
 * @since 2023-10-17 16:08:37
 */
@Service("commonFixedService")
public class CommonFixedServiceImpl extends BaseServiceImpl implements CommonFixedService {
    @Autowired
    private CommonFixedDAO commonFixedDAO;

    /**
     * 将数据表数据转换为返回数据类型
     *
     * @param entity 数据表对应类
     * @return 实例对象
     */
    protected CommonFixedVO parseEntity2VO(CommonFixed entity) {
        CommonFixedVO vo = new CommonFixedVO();
        vo.setId(entity.getId());
        vo.setVersionId(entity.getVersionId());
        vo.setPotionAmortisePeriod(entity.getPotionAmortisePeriod());
        vo.setIaAmortisePeriod(entity.getIaAmortisePeriod());
        vo.setOtherAssetsAmortisePeriod(entity.getOtherAssetsAmortisePeriod());
        vo.setLoanRepaymentPeriod(entity.getLoanRepaymentPeriod());
        vo.setFixedAssetBorrowRatio(entity.getFixedAssetBorrowRatio());
        vo.setDisposalFeeRatio(entity.getDisposalFeeRatio());
        vo.setLongTermLoanInterestRate(entity.getLongTermLoanInterestRate());
        vo.setLiquidBorrowInterestRate(entity.getLiquidBorrowInterestRate());
        vo.setLiquidBorrowInterestRatio(entity.getLiquidBorrowInterestRatio());
        vo.setLiquidOfBusinessRatio(entity.getLiquidOfBusinessRatio());
        vo.setCostInputTaxOfOptRatio(entity.getCostInputTaxOfOptRatio());
        vo.setUnitSellCost(entity.getUnitSellCost());
        vo.setSurplusReserveRatio(entity.getSurplusReserveRatio());
        vo.setExchangeRate(entity.getExchangeRate());
        vo.setTonToBarrelRatio(entity.getTonToBarrelRatio());
        vo.setOilGasEquivalent(entity.getOilGasEquivalent());
        vo.setCondensateEquivalent(entity.getCondensateEquivalent());
        vo.setOtherManageCostConventional(entity.getOtherManageCostConventional());
        vo.setOtherManageCostDeApportion(entity.getOtherManageCostDeApportion());
        vo.setOtherManageCostGround(entity.getOtherManageCostGround());
        vo.setGasManageCostConventional(entity.getGasManageCostConventional());
        vo.setGasManageCostDeApportion(entity.getGasManageCostDeApportion());
        vo.setGasManageCostGround(entity.getGasManageCostGround());
        vo.setMineralResourcesComprate(entity.getMineralResourcesComprate());
        vo.setBusinessCostRatio(entity.getBusinessCostRatio());
        vo.setSafetyCostRatioOil(entity.getSafetyCostRatioOil());
        vo.setSafetyCostRatioGas(entity.getSafetyCostRatioGas());
        vo.setOilValueAddedTaxRate(entity.getOilValueAddedTaxRate());
        vo.setGasValueAddedTaxRate(entity.getGasValueAddedTaxRate());
        vo.setOtherValueAddedTaxRate(entity.getOtherValueAddedTaxRate());
        vo.setCityConstructionTaxRate(entity.getCityConstructionTaxRate());
        vo.setEducationSurchargeRate(entity.getEducationSurchargeRate());
        vo.setResourceTaxRate(entity.getResourceTaxRate());
        vo.setIncomeTaxRate(entity.getIncomeTaxRate());
        vo.setCreateUser(entity.getCreateUser());
        vo.setCreateTime(entity.getCreateTime());
        return vo;
    }

    /**
     * 将数据传递类转换为数据表数据类
     *
     * @param dto 数据传递类实例
     * @return 实例对象
     */
    protected CommonFixed parseDTO2Entity(CommonFixedDTO dto) {
        CommonFixed entity = new CommonFixed();
        entity.setId(dto.getId());
        entity.setVersionId(dto.getVersionId());
        entity.setPotionAmortisePeriod(dto.getPotionAmortisePeriod());
        entity.setIaAmortisePeriod(dto.getIaAmortisePeriod());
        entity.setOtherAssetsAmortisePeriod(dto.getOtherAssetsAmortisePeriod());
        entity.setLoanRepaymentPeriod(dto.getLoanRepaymentPeriod());
        entity.setFixedAssetBorrowRatio(dto.getFixedAssetBorrowRatio());
        entity.setDisposalFeeRatio(dto.getDisposalFeeRatio());
        entity.setLongTermLoanInterestRate(dto.getLongTermLoanInterestRate());
        entity.setLiquidBorrowInterestRate(dto.getLiquidBorrowInterestRate());
        entity.setLiquidBorrowInterestRatio(dto.getLiquidBorrowInterestRatio());
        entity.setLiquidOfBusinessRatio(dto.getLiquidOfBusinessRatio());
        entity.setCostInputTaxOfOptRatio(dto.getCostInputTaxOfOptRatio());
        entity.setUnitSellCost(dto.getUnitSellCost());
        entity.setSurplusReserveRatio(dto.getSurplusReserveRatio());
        entity.setExchangeRate(dto.getExchangeRate());
        entity.setTonToBarrelRatio(dto.getTonToBarrelRatio());
        entity.setOilGasEquivalent(dto.getOilGasEquivalent());
        entity.setCondensateEquivalent(dto.getCondensateEquivalent());
        entity.setOtherManageCostConventional(dto.getOtherManageCostConventional());
        entity.setOtherManageCostDeApportion(dto.getOtherManageCostDeApportion());
        entity.setOtherManageCostGround(dto.getOtherManageCostGround());
        entity.setGasManageCostConventional(dto.getGasManageCostConventional());
        entity.setGasManageCostDeApportion(dto.getGasManageCostDeApportion());
        entity.setGasManageCostGround(dto.getGasManageCostGround());
        entity.setMineralResourcesComprate(dto.getMineralResourcesComprate());
        entity.setBusinessCostRatio(dto.getBusinessCostRatio());
        entity.setSafetyCostRatioOil(dto.getSafetyCostRatioOil());
        entity.setSafetyCostRatioGas(dto.getSafetyCostRatioGas());
        entity.setOilValueAddedTaxRate(dto.getOilValueAddedTaxRate());
        entity.setGasValueAddedTaxRate(dto.getGasValueAddedTaxRate());
        entity.setOtherValueAddedTaxRate(dto.getOtherValueAddedTaxRate());
        entity.setCityConstructionTaxRate(dto.getCityConstructionTaxRate());
        entity.setEducationSurchargeRate(dto.getEducationSurchargeRate());
        entity.setResourceTaxRate(dto.getResourceTaxRate());
        entity.setIncomeTaxRate(dto.getIncomeTaxRate());
        return entity;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CommonFixedVO queryById(String id) {
        CommonFixed entity = this.commonFixedDAO.selectById(id);
        if (entity == null) {
            return null;
        }
        return parseEntity2VO(entity);
    }

    /**
     * 查询多条数据
     *
     * @param dto 数据传递类实例对象
     * @return 对象列表
     */
    @Override
    public List<CommonFixedVO> queryList(CommonFixedDTO dto) {
        //mybatis-plus 写法示例
        //IPage<CommonFixed> pageResult = this.evaModelDAO.selectPage(new Page<>(dto.getPageNo(),dto.getPageSize()),null);
        //List<CommonFixed> dataList = pageResult.getRecords();
        //dto.setCount((int) pageResult.getTotal());
        List<CommonFixed> dataList = this.commonFixedDAO.queryList(dto);
        if (null == dataList || dataList.isEmpty()) {
            return Collections.emptyList();
        }
        List<CommonFixedVO> resultList = new ArrayList(dataList.size());
        for (CommonFixed item : dataList) {
            CommonFixedVO vo = parseEntity2VO(item);
            resultList.add(vo);
        }
        // TODO 添加其他关联查询数据
        return resultList;
    }

    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult create(CommonFixedDTO dto) {
        // TODO 添加校验逻辑
        CommonFixed entity = parseDTO2Entity(dto);
        preInsert(entity);
        this.commonFixedDAO.insert(entity);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult update(CommonFixedDTO dto) {
        String id = dto.getId();
        if (StringUtils.isEmpty(id)) {
            return CommonResultEnum.NOT_EXIST;
        }
        CommonFixed entity = this.commonFixedDAO.selectById(id);
        if (entity == null) {
            return CommonResultEnum.NOT_EXIST;
        }
        // TODO 添加其他校验逻辑，如数据权限

        entity.setVersionId(dto.getVersionId());
        entity.setPotionAmortisePeriod(dto.getPotionAmortisePeriod());
        entity.setIaAmortisePeriod(dto.getIaAmortisePeriod());
        entity.setOtherAssetsAmortisePeriod(dto.getOtherAssetsAmortisePeriod());
        entity.setLoanRepaymentPeriod(dto.getLoanRepaymentPeriod());
        entity.setFixedAssetBorrowRatio(dto.getFixedAssetBorrowRatio());
        entity.setDisposalFeeRatio(dto.getDisposalFeeRatio());
        entity.setLongTermLoanInterestRate(dto.getLongTermLoanInterestRate());
        entity.setLiquidBorrowInterestRate(dto.getLiquidBorrowInterestRate());
        entity.setLiquidBorrowInterestRatio(dto.getLiquidBorrowInterestRatio());
        entity.setLiquidOfBusinessRatio(dto.getLiquidOfBusinessRatio());
        entity.setCostInputTaxOfOptRatio(dto.getCostInputTaxOfOptRatio());
        entity.setUnitSellCost(dto.getUnitSellCost());
        entity.setSurplusReserveRatio(dto.getSurplusReserveRatio());
        entity.setExchangeRate(dto.getExchangeRate());
        entity.setTonToBarrelRatio(dto.getTonToBarrelRatio());
        entity.setOilGasEquivalent(dto.getOilGasEquivalent());
        entity.setCondensateEquivalent(dto.getCondensateEquivalent());
        entity.setOtherManageCostConventional(dto.getOtherManageCostConventional());
        entity.setOtherManageCostDeApportion(dto.getOtherManageCostDeApportion());
        entity.setOtherManageCostGround(dto.getOtherManageCostGround());
        entity.setMineralResourcesComprate(dto.getMineralResourcesComprate());
        entity.setBusinessCostRatio(dto.getBusinessCostRatio());
        entity.setSafetyCostRatioOil(dto.getSafetyCostRatioOil());
        entity.setSafetyCostRatioGas(dto.getSafetyCostRatioGas());
        entity.setOilValueAddedTaxRate(dto.getOilValueAddedTaxRate());
        entity.setGasValueAddedTaxRate(dto.getGasValueAddedTaxRate());
        entity.setOtherValueAddedTaxRate(dto.getOtherValueAddedTaxRate());
        entity.setCityConstructionTaxRate(dto.getCityConstructionTaxRate());
        entity.setEducationSurchargeRate(dto.getEducationSurchargeRate());
        entity.setResourceTaxRate(dto.getResourceTaxRate());
        entity.setIncomeTaxRate(dto.getIncomeTaxRate());
        this.commonFixedDAO.updateById(entity);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 操作结果
     */
    @Override
    public ServiceResult deleteById(String id) {
        // TODO 添加校验逻辑，如数据权限
        this.commonFixedDAO.deleteById(id);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public ServiceResult save(CommonFixedDTO dto) {
        deleteByVersion(dto.getVersionId());
        return create(dto);
    }

    @Override
    public CommonFixedVO queryByVersion(String versionId) {
        CommonFixedDTO commonFixedDTO = new CommonFixedDTO();
        commonFixedDTO.setVersionId(versionId);
        List<CommonFixedVO> commonFixedVOList = queryList(commonFixedDTO);
        if(commonFixedVOList!=null&&commonFixedVOList.size()>0) {
            return commonFixedVOList.get(0);
        }
        return null;
    }

    @Override
    public void copy(String fromId, String toId) {
        final CommonFixedDTO dto = new CommonFixedDTO();
        dto.setVersionId(fromId);
        final List<CommonFixed> commonFixeds = commonFixedDAO.queryList(dto);
        if (CollectionUtils.isEmpty(commonFixeds)) {
            return;
        }

        final List<CommonFixed> collect = commonFixeds.stream().map(m -> {
            final CommonFixed deepCopy = BusiUtil.deepCopy(m, CommonFixed.class);
            deepCopy.setId(IdGen.uuid());
            deepCopy.setCreateTime(new Date());
            deepCopy.setVersionId(toId);
            return deepCopy;
        }).collect(Collectors.toList());

        commonFixedDAO.insertBatch(collect);
    }

    @Override
    public ServiceResult deleteByVersion(String versionId) {
        QueryWrapper<CommonFixed> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CommonFixed::getVersionId, versionId);
        commonFixedDAO.delete(queryWrapper);
        return CommonResultEnum.SUCCESS;
    }


}
